<template>
	<view>
		<view class="header">
			<view class="bg">
				<view class="box">
					<view class="box-hd">
						<view @click="toShowMessageList">
							<img src="/static/tongzhi.png" class="msg">
							<view class="msg-count" v-if="messageCount!=0">{{messageCount}}</view>
						</view>
						<view class="avator" v-if="coach!=null">
							<img :src="coach.coachHeadPortrait" class="avatarPic" />
						</view>
						<view class="phone-number" v-if="coach!=null">{{coach.coachNickname}}</view>
					</view>
					<view class="box-bd" v-if="coach!=null">
						<view class="item" @click="toFocusList">
							<view class="text"><b>{{coachIndexCount.focusCount}}</b></view>
							<view class="text">
								<font color="#666">关注</font>
							</view>
						</view>
						<view class="item" @click="toFansList">
							<view class="text"><b>{{coachIndexCount.fansCount}}</b></view>
							<view class="text">
								<font color="#666">粉丝</font>
							</view>
						</view>
						<view class="item" @click="toPostList">
							<view class="text"><b>{{coachIndexCount.postCount}}</b></view>
							<view class="text">
								<font color="#666">帖子</font>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="list-content">
			<view class="list">
				<view class="li" @tap="toPage(row.url)" v-for="(row,index) in functionList" :key="index">
					<view class="icon">
						<image :src="row.img"></image>
					</view>
					<view class="text">{{row.text}}</view>
					<image class="to" src="/static/to.png"></image>
				</view>
			</view>
		</view>
		<!-- 教练课程提示-->
		<view class="toolbar" v-if="isExistDayWorkPlan">
			<view class="title" style="display: flex;justify-content: center;">今日课程</view>
			<view style="display: flex;justify-content: center;" class="list" v-for="(dayWorkPlan,index) in dayWorkPlanList"
			 :key="index">
				{{dayWorkPlan.time}}|{{dayWorkPlan.curseName}}
			</view>
		</view>
		<!-- 占位 -->
		<view class="place-bottom"></view>
	</view>
</template>
<script>
	import base from '@/common/base.js'
	export default {
		data() {
			return {
				messageLoop: false,
				isfirst: true,
				headerPosition: "fixed",
				headerTop: null,
				statusTop: null,
				showHeader: true,
				coach: null,
				interval:null,
				messageCount: base.messageCount,
				//个人信息,
				user: {
					username: '游客1002',
					face: '/static/img/face.jpg',
					integral: 0,
					balance: 0,
					envelope: 0
				},
				// 功能列表
				functionList: [{
						url: '../../user/stadium/show',
						text: '我的场馆',
						img: '/static/img/user/stadium.png'
					},
					{
						url: '../../user/course/publish',
						text: '课程发布',
						img: '/static/img/user/coursePublish.png'
					},
					{
						url: '../../user/course/courseList',
						text: '我的课程',
						img: '/static/img/user/myCourse.png'
					},
					{
						url: '../../user/course/historyCourse',
						text: '历史课程查询',
						img: '/static/img/user/myBill.png'
					},
					{
						url: '../../user/setting/list',
						text: '设置',
						img: '/static/img/user/set.png'
					}

				],
				dayWorkPlanList: [],
				isExistDayWorkPlan: false,
				coachIndexCount: []
			}
		},
		//下拉刷新，需要自己在page.json文件中配置开启页面下拉刷新 "enablePullDownRefresh": true
		onPullDownRefresh() {
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		onPageScroll(e) {
			//兼容iOS端下拉时顶部漂移
			this.headerPosition = e.scrollTop >= 0 ? "fixed" : "absolute";
			this.headerTop = e.scrollTop >= 0 ? null : 0;
			this.statusTop = e.scrollTop >= 0 ? null : -this.statusHeight + 'px';
		},
		onLoad() {
			if (base.coach != null) {
				this.coach = base.coach;
				this.getDayWorkPlanList();
				this.getIndexCount();
			} else {
				this.isExistDayWorkPlan = false;
			}
			this.statusHeight = 0;
			// #ifdef APP-PLUS
			this.showHeader = false;
			this.statusHeight = plus.navigator.getStatusbarHeight();
			// #endif
		},
		onReady() {

			//此处，演示,每次页面初次渲染都把登录状态重置
			uni.setStorage({
				key: 'UserInfo',
				data: false,
				success: function() {},
				fail: function(e) {}
			});
		},
		onShow() {
			this.coach = base.coach;
			this.messageLoop = true;
			this.getMessageCount();
			this.getIndexCount();
			uni.getStorage({
				key: 'UserInfo',
				success: (res) => {
					if (!res.data) {
						if (this.isfirst) {
							//this.toLogin();
						}
						return;
					}
					this.user = res.data;
				},
				fail: (e) => {
					//this.toLogin(); 
				}
			});
		},
		onHide() {
			this.messageLoop = false;
			this.getMessageCount();
		},
		methods: {
			toFocusList() {
				uni.navigateTo({
					url: '/pages/user/focus/focusList'
				})
			},
			toFansList() {
				uni.navigateTo({
					url: '/pages/user/fans/fansList'
				})
			},
			getIndexCount() {
				uni.request({
					url: base.urlPrefix + 'yoga_app/focus/coachGetIndexCount',
					data: {
						coachId: base.coach.coachId
					},
					success: (res) => {
						this.coachIndexCount = res.data.object;
					}
				})
			},
			//获取教练今日课表的方法
			getDayWorkPlanList() {
				uni.request({
					url: base.urlPrefix + 'yoga_app/curse-plan/coachGetDayWorkPlanListByCoachId',
					data: {
						coachId: base.coach.coachId
					},
					success: (res) => {
						if (res.data.code == "success") {
							this.dayWorkPlanList = res.data.list;
							this.isExistDayWorkPlan = true;
						}
					}
				})
			},
			toPostList() {
				uni.navigateTo({
					url: '/pages/user/post/postList'
				})
			},
			//跳转到显示消息列表的方法
			toShowMessageList() {
				uni.navigateTo({
					url: '/pages/user/message/messageList'
				})
			},
			getMessageCount() {
				if (!this.messageLoop) {
					if(this.interval!=null){
						clearInterval(this.interval);
					}
					console.log("关闭");
					return;
				} else {
					this.interval=setInterval(() => {
						uni.request({
							url: base.urlPrefix + "yoga_app/message-record/coachGetMessageRecordCount",
							data: {
								coachId: base.coach.coachId
							},
							success: (res) => {
								this.messageCount = res.data.object;
							}
						})
					}, 3000);
				}
			},
			//功能区跳转页面的方法，如果用户未登录，提示用户先登录
			toPage(url) {
				uni.navigateTo({
					url: url
				})
			}

		}
	}
</script>
<style lang="scss">
	page {
		background-color: #EEEEEE;
		font-size: 30upx;
	}

	.header {
		position: relative;
		margin-top: 0;
		height: 500upx;
		width: 100%;

		.bg {
			position: absolute;
			top: 0;
			width: 100%;
			height: 300upx;
			background-color: #4191ea;
		}
	}

	.box {
		position: relative;
		width: 650upx;
		height: 280upx;
		border-radius: 20upx;
		margin: 150rpx auto;
		margin-bottom: 0;
		background: #fff;
		box-shadow: 0 5upx 20upx 0upx rgba(0, 0, 150, .2);

		.box-hd {
			position: relative;
			display: flex;
			flex-wrap: wrap;
			flex-direction: row;
			justify-content: center;

			.avator {
				width: 160upx;
				height: 160upx;
				background: #fff;
				border: 5upx solid #fff;
				border-radius: 50%;
				margin-top: -80upx;
				overflow: hidden;

				img {
					width: 100%;
					height: 100%;
				}
			}

			.phone-number {
				width: 100%;
				text-align: center;
			}
		}

		.box-bd {
			display: flex;
			flex-wrap: nowrap;
			flex-direction: row;
			justify-content: center;

			.item {
				flex: 1 1 auto;
				display: flex;
				flex-wrap: wrap;
				flex-direction: row;
				justify-content: center;
				border-right: 1px solid #f1f1f1;
				margin: 15upx 0;

				&:last-child {
					border: none;
				}

				.icon {
					width: 60upx;
					justify-content: center;

					img {
						width: 100%;
						height: 100%;
					}
				}

				.text {
					width: 100%;
					text-align: center;
					margin-top: 10upx;
				}
			}
		}
	}

	.list-content {
		background: #fff;
	}

	.list {
		width: 100%;
		border-bottom: 15upx solid #f1f1f1;
		background: #fff;

		&:last-child {
			border: none;
		}

		.li {
			width: 92%;
			height: 100upx;
			padding: 0 4%;
			border-bottom: 1px solid rgb(243, 243, 243);
			display: flex;
			align-items: center;

			&.noborder {
				border-bottom: 0
			}

			.icon {
				flex-shrink: 0;
				width: 50upx;
				height: 100upx;
				line-height: 100upx;

				image {
					width: 50upx;
					height: 50upx;
				}
			}

			.text {
				padding-left: 20upx;
				width: 100%;
				color: #666;
			}

			.to {
				flex-shrink: 0;
				width: 40upx;
				height: 40upx;
			}
		}
	}

	.function-explain {
		font-size: 20upx;
		display: block;
		float: right;
		font-weight: 100;
		text-align: center;
		line-height: 50upx;
	}

	.msg {
		height: 50rpx;
		width: 50rpx;
		position: absolute;
		top: 30rpx;
		right: 35rpx;
	}

	.msg-count {
		position: absolute;
		top: 10rpx;
		right: 20rpx;
		width: 40rpx;
		height: 40rpx;
		border-radius: 20rpx;
		background-color: red;
		line-height: 40rpx;
		text-align: center;
		color: #fff;
	}

	.avatarPic {
		width: 160rpx;
		height: 160rpx;
		border-radius: 50%;
	}
</style>
